package com.jyh.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

import com.alibaba.fastjson.JSONObject;



/**
 * 
 * @author jiwei
 *
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{

	/**
	 * 判断是否只包含字母和数字
	 * 
	 * @param str
	 * @return boolean true只包含字母和数字，false则相反
	 */
	public static boolean isNumOrLetter(String str) {
		if (str == null) {
			return false;
		}
		return str.matches("^[a-zA-Z0-9]+$");
	}

	/**
	 * 判断当前字符串是否为null
	 * 
	 * @param str
	 * @return boolean true当前字符串为null，false不为null
	 */
	public static boolean isNull(String str) {
		return str == null || "".equals(str) || "null".equals(str);
	}
	
	/**
	 * 判断当前字符串是否不为null
	 * 
	 * @param str
	 * @return boolean true当前字符串不为null，false为null
	 */
	public static boolean isNotNull(String str) {
		return str != null && !"".equals(str) && !"null".equals(str);
	}

	/**
	 * 判断当前字符串是否为double类型
	 * 
	 * @param str
	 * @return boolean true当前字符串为double，false不为double
	 */
	public static boolean isDouble(String str) {
		try {
			Double.parseDouble(str);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 判断当前字符串是否为Int类型
	 * 
	 * @param str
	 * @return boolean true当前字符串为Int，false不为Int
	 */
	public static boolean isInt(String str) {
		try {
			Integer.parseInt(str);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static int strToInt(String str) {
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
			return 0;
		}
	}

	public static double strToDouble(String str) {
		try {
			return Double.parseDouble(str);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 用于获取一个String的md5值
	 * 
	 * @param string
	 * @return
	 */
	public static String getMd5(String str) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		byte[] bs = md5.digest(str.getBytes());
		StringBuilder sb = new StringBuilder(40);
		for (byte x : bs) {
			if ((x & 0xff) >> 4 == 0) {
				sb.append("0").append(Integer.toHexString(x & 0xff));
			} else {
				sb.append(Integer.toHexString(x & 0xff));
			}
		}
		return sb.toString();
	}

	/**
	 * 邮件的MD5
	 * jiwei
	 * 2017年3月30日上午10:25:53
	 * @param plainText
	 * @return
	 */
	public static String getmailMd5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
	
	
	
	
	/**
	 * 将字符串数组转换为字符串
	 * 
	 * @param strs
	 * @return String
	 */
	public static String arrayToStr(String strs[]) {
		if (strs == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (String str : strs) {
			sb.append("" + str + "").append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}
	/**
	 * 将字符串数组转换为sql字符串的in条件字符串
	 * 
	 * @param strs
	 * @return String
	 */
	public static String arrayToInSql(String strs[]) {
		if (strs == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (String str : strs) {
			sb.append("'" + str + "'").append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}
	/**
	 * 将json字符串转换成json对象
	* @Title: getobj 
	* @Description: TODO
	* @param @param str
	* @param @return    
	* @return JSONObject    
	* @throws 
	* @author kiwishang 
	* @date 2017年9月20日 上午10:49:55
	 */
   public static JSONObject getobj(String str){
	   JSONObject resObj = null;
		try {
			resObj = JSONObject.parseObject(str);
		} catch (Exception e) {
			resObj = JSONObject.parseObject("{\"result\":\"error\",\"error\":\"json转换失败\"}");
		}
		return resObj;
   }
   /**
    * 解码base64编码的数据
   * @Title: gunzip 
   * @Description: TODO
   * @param @param compressedStr
   * @param @return    
   * @return String    
   * @throws 
   * @author kiwishang 
   * @date 2017年9月20日 下午2:45:33
    */
   public static  String gunzip(String compressedStr){
	   ByteArrayOutputStream out= new ByteArrayOutputStream(); 
	   ByteArrayInputStream in=null; 
       GZIPInputStream ginzip=null; 
       byte[] compressed=null; 
       String decompressed = null;         
       try { // 对返回数据BASE64解码           
    	   try {
			compressed = decodeBase.decodeBase64(compressedStr);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    	     in=new ByteArrayInputStream(compressed);
             ginzip=new GZIPInputStream(in); // 解码后对数据gzip解压缩    
	         byte[] buffer = new byte[1024]; 
	         int offset = -1; 
	         while ((offset = ginzip.read(buffer)) != -1) { 
             out.write(buffer, 0, offset); 
             }             
              decompressed=out.toString("utf-8"); 
              } catch (IOException e) { 
            	 e.printStackTrace();
              }    
             return decompressed; 
          }
   
   
   
   /**
    * 对给定的字符串进行base64解码操作
    */
   public static String decodeData(String inputData) {
       try {
           if (null == inputData) {
               return null;
           }
           return new String(Base64.decodeBase64(inputData.getBytes("UTF_8")), "UTF_8");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
           
       }

       return null;
   }

   /**
    * 对给定的字符串进行base64加密操作
    */
   public static String encodeData(String inputData) {
       try {
           if (null == inputData) {
               return null;
           }
           return new String(Base64.encodeBase64(inputData.getBytes("UTF_8")), "UTF_8");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }

       return null;
   }

   /**
    * 将字符串转成utf-8编码
   * @Title: getutf8code 
   * @Description: TODO
   * @param @return    
   * @return String    
   * @throws 
   * @author kiwishang 
   * @date 2017年10月11日 下午2:14:21
    */
   public static String getutf8code(String str){
	   String xmlUTF8 ="";
	   try{
		String xmString = new String(str.getBytes("UTF-8"));  
	    xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");  
	    } catch (UnsupportedEncodingException e) {  
	     e.printStackTrace();  
	    }  
	   return xmlUTF8;
   }
}
